package com.example.job_backend.controller;

import com.example.job_backend.model.Query.ResumeQuery;
import com.example.job_backend.model.Query.ResumeRequest;
import com.example.job_backend.model.R;
import com.example.job_backend.model.Resume;
import com.example.job_backend.service.ResumeService;
import com.example.job_backend.util.AliOSSUtils;
import com.example.job_backend.util.EmailSendUtils;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ResumeController {
    @Resource
    private ResumeService resumeService;
    @Autowired
    private EmailSendUtils emailSendUtils;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    //小程序用
    @GetMapping("/resumes")
    public R getResumeByUserName(Integer student_id) {
        List<Resume> resumeList = resumeService.findResumeByUsername(student_id);
        return new R(200, "请求成功", resumeList);
    }

    //系统管理用
    @GetMapping("/getResumes")
    public R getResumes(ResumeQuery query) {
        PageInfo<Resume> list = resumeService.getResumeByPage(query);
        return new R(200, "请求成功", list);
    }

    @GetMapping("/saveResume")
    public R saveResume(Resume resume) {
        resumeService.save(resume);
        return new R(200, "请求成功", null);
    }

    @GetMapping("/deleteResume/{id}")
    public R deleteResume(@PathVariable("id") Integer id) {
        resumeService.removeById(id);
        return new R(200, "删除成功", null);
    }

    @GetMapping("/updateResume")
    public R updateResume(Resume resume) {
        resumeService.updateById(resume);
        return new R(200, "更新成功", null);
    }

    @PostMapping("/uploadResume")
    public R uploadResume(MultipartFile file) throws IOException {
        String url = aliOSSUtils.uploadFile(file);
        return new R(200, "请求成功", url);
    }

    @PostMapping("/createResume")
    public R createResume(@RequestBody ResumeRequest request) throws IOException {
        // 使用 DTO 对象接收参数
        // 确保字段命名与前端一致（如educations/internships等）
        request.setBirthDate(request.getBirthDateYMD());
        ResumeRequest.EducationDTO educationDTO=request.getEducations().get(0);
        System.out.println(educationDTO.getStartDate());
        resumeService.createResume(request);
        return new R(200, "请求成功", null);
    }

    @PostMapping("/sendEmail")
    public R sendResume(@RequestParam("email") String email, @RequestParam("resume_id") Integer resume_id) {
        try {
            //获取简历文件
            Resume resume = resumeService.getById(resume_id);
            String fileUrl =resume.getFilePath();
            File file=aliOSSUtils.downloadByUrl(fileUrl);

            //发送邮件
            emailSendUtils.sendResume(email,
                    "简历投递 - " + resume.getTitle(),
                    "请查收附件简历",
                    file
            );

            return new R(200, "请求成功", null);
        } catch (Exception e) {
            return new R(500, "发送失败", e.getMessage());
        }
    }
}
